/**
 * Copyright 2018-2020 stylefeng & fengshuonan (https://gitee.com/stylefeng)
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.stylefeng.guns.modular.comm.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.guns.base.oshi.SystemHardwareInfo;
import cn.stylefeng.guns.base.shiro.ShiroUser;
import cn.stylefeng.guns.comm.util.CommConstant;
import cn.stylefeng.guns.modular.comm.model.UploadResult;
import cn.stylefeng.guns.sys.core.constant.factory.ConstantFactory;
import cn.stylefeng.guns.sys.core.log.LogObjectHolder;
import cn.stylefeng.guns.sys.core.properties.GunsProperties;
import cn.stylefeng.guns.sys.core.shiro.ShiroKit;
import cn.stylefeng.guns.sys.modular.system.entity.FileInfo;
import cn.stylefeng.guns.sys.modular.system.entity.Notice;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.FileInfoService;
import cn.stylefeng.guns.sys.modular.system.service.NoticeService;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.reqres.response.SuccessResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.RequestEmptyException;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import cn.stylefeng.roses.kernel.model.exception.enums.CoreExceptionEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.URLDecoder;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.List;

/**
 * 通用控制器
 *
 * @author fengshuonan
 * @Date 2017年2月17日20:27:22
 */
@Controller
@RequestMapping("/system")
@Slf4j
public class UploadController extends BaseController {

    @Autowired
    private FileInfoService fileInfoService;


    @Autowired
    private GunsProperties gunsProperties;

//    /**
//     * layui上传组件 通用文件上传接口
//     *
//     * @author fengshuonan
//     * @Date 2019-2-23 10:48:29
//     */
//    @RequestMapping(method = RequestMethod.POST, path = "/upload")
//    @ResponseBody
//    public ResponseData layuiUpload(@RequestPart("file") MultipartFile file, String type) {
//
//        String fileId  = this.fileInfoService.uploadFile(file);
//        //String fileId = uploadResult.getFileId();
//
//        HashMap<String, Object> map = new HashMap<>();
//        map.put("fileId", fileId);
//
//        return ResponseData.success(0, "上传成功", map);
//    }

    /**
     * layedit富文本上传组件 通用文件上传接口
     * {
     * "code": 0 //0表示成功，其它失败
     * ,"msg": "" //提示信息 //一般上传失败后返回
     * ,"data": {
     * "src": "图片路径"
     * ,"title": "图片名称" //可选
     * }
     * }
     *
     * @author fengshuonan
     * @Date 2019-2-23 10:48:29
     */
    @RequestMapping(method = RequestMethod.POST, path = "/layedit/upload")
    @ResponseBody
    public Object layeditUpload(@RequestPart("file") MultipartFile file) {

        String fileId = this.fileInfoService.uploadFile(file);

        FileInfo fileInfo = fileInfoService.getById(fileId);

        HashMap<String, Object> map = new HashMap<>();
        map.put("code", 0);
        HashMap<String, Object> data = new HashMap<>();
        data.put("src", CommConstant.FILE_PATH + fileInfo.getFilePath().substring(gunsProperties.getFileUploadPath().length() - 1).replaceAll("\\\\", "/"));
        map.put("data", data);

        return map;
    }


}
